﻿using System;
using System.Data;
using System.Drawing;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dundas.Charting.WebControl;
using LenderClearingHouse.Helpers;

namespace LenderClearingHouse
{
    public partial class Dashboard2 : PageBase
    {
        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlPropertyType.Items.Add("(all)");
                ddlPropertyType.Items.Add("OTH");
                ddlPropertyType.Items.Add("SFR");
                ddlPropertyType.Items.Add("4Px");
                ddlPropertyType.Items.Add("APT");
                ddlPropertyType.Items.Add("TWN");
                ddlPropertyType.Items.Add("CON");
                ddlPropertyType.Items.Add("CMM");
                ddlPropertyType.Items.Add("DUP");
                ddlPropertyType.Items.Add("HIR");
            }

            var listDate = DateValidator.ValidateCalendarDate(txtFromDate.Text, txtToDate.Text, CalendarFromDate.SelectedDate, CalendarToDate.SelectedDate);
            txtFromDate.Text = listDate.ElementAt(0).ToString();
            txtToDate.Text = listDate.ElementAt(1).ToString();
            CalendarFromDate.SelectedDate = Convert.ToDateTime(listDate.ElementAt(2));
            CalendarToDate.SelectedDate = Convert.ToDateTime(listDate.ElementAt(3)); 
            
            Chart1.ChartAreas["Default"].AxisX.Interval = 1;
            Chart1.ChartAreas["Default"].AxisY.Interval = 1;
            Chart1.Series[0].Type = SeriesChartType.Line;
            Chart1.Series[0].MarkerStyle = MarkerStyle.Square;
            Chart1.Series[0].MarkerSize = 10;

            Chart1.Series[0].EmptyPointStyle.BorderWidth = 0;
            Chart1.Series[0].EmptyPointStyle.MarkerStyle = MarkerStyle.None;
            //Chart1.Series[0].EmptyPointStyle.BorderStyle = ChartDashStyle.Dot;
            //Chart1.Series[0].EmptyPointStyle.Color = Chart1.Series[0].Color;

            Chart1.Series[0].Points.Clear();
            
            if (!this.Page.IsPostBack)
            {
                // Set initial X axis zooming
                //this.Chart1.ChartAreas["Default"].AxisX.View.Position = -1;
                //this.Chart1.ChartAreas["Default"].AxisX.View.Size = 7.5;

                FillChart();

                User u = Session["User"] as User;
                lbIvestor.Visible = u.UserRole == UserRole.SuperAdmin;
                cbInvestor.Visible = lbIvestor.Visible;
            }
        }

        private void FillChart()
        {
            User u = Session["User"] as User;
            DateTime fromDate = CalendarFromDate.SelectedDate.HasValue ? CalendarFromDate.SelectedDate.Value : DateTime.Now;
            DateTime toDate = CalendarToDate.SelectedDate.HasValue ? CalendarToDate.SelectedDate.Value : DateTime.Now; 

            int investorId = -1;
            if (u.UserRole == UserRole.SuperAdmin)
            {
                investorId = cbInvestor.SelectedIndex != -1 ? int.Parse(cbInvestor.SelectedValue) : -1;
            }
            else
            {
                investorId = int.Parse(u.UserId);
            }

            string propertyType = null;
            if (ddlPropertyType.SelectedIndex > 0)
            {
                propertyType = ddlPropertyType.SelectedValue;
            }

            DataAccess da = new DataAccess();
            DataTable tbl = da.GetDataTable("getDashboard2",
                new string[] { "@InvestorId", "@TypeView", "@propertyType", "@fromDate", "@toDate" },
                new object[] { investorId, ddlView.SelectedValue, propertyType, fromDate, toDate }); 
               
            int i = 0;
            foreach (DataRow dr in tbl.Rows)
            {
                if (dr.IsNull("CountProp"))
                    Chart1.Series[0].Points.AddXY(i, double.NaN);
                else
                    Chart1.Series[0].Points.AddXY(i, Convert.ToInt32(dr["CountProp"]));

                if (ddlView.SelectedValue == "1")
                {
                    System.Globalization.DateTimeFormatInfo fi = new System.Globalization.DateTimeFormatInfo();
                    string strMonthName = fi.GetAbbreviatedMonthName(Convert.ToInt32(dr["IdSub"])).ToString();
                    Chart1.Series[0].Points[i].AxisLabel = strMonthName + " " + dr["IdYear"].ToString();
                }
                else if (ddlView.SelectedValue == "2")
                    Chart1.Series[0].Points[i].AxisLabel = "Q" + dr["IdSub"].ToString() + " " + dr["IdYear"].ToString();
                else if (ddlView.SelectedValue == "3")
                    Chart1.Series[0].Points[i].AxisLabel = dr["IdYear"].ToString();

                ++i;
            }
        }

        protected void ddlView_SelectedIndexChanged(Object sender, EventArgs e)
        {
            FillChart();
        }

        protected void cbInvestor_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            FillChart();
        }

        protected void calendarDate_Changed(object sender, System.EventArgs e)
        {
            lblDataValidate.Style.Add("display", "none");
            FillChart();
        }   
    }
}